var	 app = getApp();
Page({
  data: {
  	errImg:app.globalData.errimg,
  	id:'',
    page: 1,
    pages: 100,
    limit:10,
    goodsList: [],
    keys:'',
    recommend:0
  },
  returntext:function(a){
  	var aaa='1111'
  	return aaa;
  },
  onLoad(options) {
  	if(options.id){
  		this.data.id=options.id
  	}
  	console.log(options.id);
  	if(options.keys){
  		this.setData({
		      keys: options.keys
		  })
  	}
  	console.log(options.recommend)
  	if(options.recommend){
  		this.setData({
		      recommend: 1
		  })
  	}
  	
    // 页面初次加载，请求第一页数据
    this.fetchArticleList(1, true,function(e){})
    
  },
  searchIcon(e) {//输入
  	this.data.keys=e.detail.value;
  },
  search(){
  	console.log(this.data.keys);
  	if(this.data.keys==""){
  		wx.showToast({
        title: "请输入商品名称",
        icon: "none"});
  		return false;
  	}
  	this.setData({
      keys: this.data.keys
    })
  	this.fetchArticleList(1, true,function(e){})
  },
  ReachBottom() {
  	console.log("触底")
    // 下拉触底，先判断是否有请求正在进行中
    // 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
    if (!this.loading && this.data.page < this.data.pages) {
    	this.data.page+=1;
      this.fetchArticleList(this.data.page, false,function(e){})
    }
  },
  onPullDownRefresh() {
    // 上拉刷新
    console.log(this.loading)
    if (!this.loading) {
      this.fetchArticleList(1, true,function(){
      	wx.stopPullDownRefresh();
      })
    }
  },
  fetchArticleList(pageNo, override,call) {
  	var $th=this;
    $th.loading = true
    // 向后端请求指定页码的数据
    console.log(pageNo, override,call)
    if(override){//重置list
    	$th.data.goodsList=[];
    }
    if(call){
    	var post={
    		categoryId:$th.data.id,
    		page:pageNo,
    		limit:$th.data.limit
    	}
    	
    	if($th.data.keys){//搜索
    	  delete	post.categoryId
    	  post.goodsName=$th.data.keys
    	}
    	
    	if($th.data.recommend){//热销
    	  post.isRecommend=1
    	}
    	app.globalAjax(
	  		'/api/goods/list',
	  		"get",
	  		post,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		$th.setData({
					      goodsList: $th.data.goodsList.concat(e.goodsList)
					  })
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	})
    	
    	setTimeout(function(){
    		$th.loading=false;
    		call();
    	},1000)
    }
  },
  gotourl(s){
	  	
	 },errImg: function(e){
    var _errImg=e.target.dataset.errImg;
    var _objImg="'"+_errImg+"'";
    var _errObj={};
    _errObj[_errImg]=this.data.errImg;
    console.log( e.detail.errMsg+"----"+ _errObj[_errImg] + "----" +_objImg );
    this.setData(_errObj);//注意这里的赋值方式...
  }
})